/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Facades;

import VOs.City;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Julian
 */
@Stateless
public class CityFacade implements CityFacadeRemote {
    @PersistenceContext(unitName = "AccesoDatosPU")
    private EntityManager em;

    public void create(City city) throws Exception{
        try {
        em.persist(city);    
        } catch (Exception e) {
            throw e;
        }

    }

    public void edit(City city) throws Exception{
        try {
        em.merge(city);
        } catch (Exception e) {
            throw e;
        }
        
    }

    public void remove(City city) throws Exception{
        try {
        em.remove(em.merge(city));    
        } catch (Exception e) {
            throw e;
        }

    }

    public City find(Object id) throws Exception{
        try {
        return em.find(City.class, id);
        } catch (Exception e) {
            throw e;
        }
        
    }

    public List<City> findAll() throws Exception{
        try {
        return em.createQuery("select object(o) from City as o").getResultList();
        } catch (Exception e) {
            throw e;
        }
        
    }

    public List<City> findRange(int[] range) {
        Query q = em.createQuery("select object(o) from City as o");
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        return ((Long) em.createQuery("select count(o) from City as o").getSingleResult()).intValue();
    }

    public City findid(int id) throws Exception
    {
        try {
        Query query = em.createNamedQuery("City.findById");
       query.setParameter("id", id);

        return (City) query.getSingleResult();
        } catch (Exception e) {
            throw e;
        }

    }
}
